package io.gitee.zhangbinhub.acp.boot.init

import io.gitee.zhangbinhub.acp.boot.component.SystemControl
import io.gitee.zhangbinhub.acp.boot.daemon.DaemonServiceManager
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.acp.boot.socket.InitTcpServer
import io.gitee.zhangbinhub.acp.boot.socket.InitUdpServer
import io.gitee.zhangbinhub.acp.boot.tools.ServerTools
import io.gitee.zhangbinhub.acp.core.common.CommonTools
import reactor.core.publisher.Hooks

/**
 * 系统初始化
 * Created by zhangbin on 2017-6-16.
 */
class SystemInitialization(
    private val logAdapter: LogAdapter,
    private val serverTools: ServerTools,
    private val systemControl: SystemControl,
    private val initTcpServer: InitTcpServer,
    private val initUdpServer: InitUdpServer
) : AcpInitialization {

    override val name: String
        get() = "System Initialization"

    override val order: Int
        get() = Int.MIN_VALUE

    override fun doInit() {
        logAdapter.info("<<<<<<<<<<<<<<<<<<<< System is starting ... >>>>>>>>>>>>>>>>>>>>")
        Hooks.enableAutomaticContextPropagation()
        /* 启动初始化socket服务 */
        initTcpServer.startTcpServer()
        initUdpServer.startUdpServer()
        /* 启动 listener 及定时任务 */
        systemControl.initialization()
        Runtime.getRuntime().addShutdownHook(Thread { DaemonServiceManager.stopAllService() })
        logAdapter.info("<<<<<<<<<<<<<<<<<<<< System startup completed on port ${serverTools.getServerPort()}! Default charset is ${CommonTools.getDefaultCharsetName()} >>>>>>>>>>>>>>>>>>>>")
    }
}